package wangyihang.test;

import com.alibaba.excel.EasyExcel;
import com.google.common.collect.Lists;
import wangyihang.dto.ExcelData;
import wangyihang.listener.DemoDataListener;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

public class DoExcel {

    public static List<ExcelData> list1 = new ArrayList<ExcelData>();
    public static List<ExcelData> list2 = new ArrayList<ExcelData>();
    public static String totalString1 = "";
    public static String totalString2 = "";

    public static void main(String[] args) {

//        String fileName = getPath() + "demo" + File.separator + "demo.xlsx";
        String fileName1 = "/Users/apple/Downloads/title.xlsx";
        EasyExcel.read(fileName1, ExcelData.class, new DemoDataListener()).sheet().doRead();
        list1 = DemoDataListener.list;
        for (ExcelData excelData : list1) {
            totalString1 = totalString1 + ";" + excelData.getData();
        }
        DemoDataListener.list.clear();

        String fileName2 = "/Users/apple/Downloads/keyWords.xlsx";
        EasyExcel.read(fileName2, ExcelData.class, new DemoDataListener()).sheet().doRead();
        list2 = DemoDataListener.list;
        for (ExcelData excelData : list2) {
            totalString2 = totalString2 + ";" + excelData.getData();
        }
        wirteExcel(totalString1, totalString2);
    }

    private static void wirteExcel(String totalString1, String totalString2) {
        int index = 0;
        List<ExcelData> datas = new ArrayList<ExcelData>();
        List<String> total1 = Lists.newArrayList(totalString1.split(";"))
                .stream().filter(e -> !e.equals("")).collect(Collectors.toList());
        List<String> total2 = Lists.newArrayList(totalString2.split(";"))
                .stream().filter(e -> !e.equals("")).collect(Collectors.toList());

        Iterator<String> iterator = total1.iterator();
        String title = "";
        for (String key : total2) {
            if (index % 3 == 0) {
                if (iterator.hasNext()) {
                    datas.add(ExcelData.builder().data(title).build());
                    title = iterator.next();
                }
            }
            if (!title.contains(key)) {
                title = key + " " + title + " ";
            }
            datas.add(ExcelData.builder().data(key).build());
            index++;
        }

        /*for (String data : total2) {
            if (index % 3 == 0 && index != 0) {
                if (iterator.hasNext()) {
                    datas.add(ExcelData.builder().data(iterator.next()).build());
                }
            }
            datas.add(ExcelData.builder().data(data).build());
            index++;
        }*/

        String templateFileName = "/Users/apple/Downloads/template.xlsx";
        String fileName = "/Users/apple/Downloads/done1.xlsx";
        EasyExcel.write(fileName).withTemplate(templateFileName).sheet().doFill(datas);
    }

//    @Test
    public void subWirte() {
        String doneFile = "/Users/apple/Downloads/done.xlsx";
        List<String> list = new ArrayList<>();
        EasyExcel.read(doneFile, ExcelData.class, new DemoDataListener()).sheet().doRead();
        DemoDataListener.list.forEach(e -> list.add(e.getData()));
        System.out.println("done");
    }
}
